package com.htt.app.xonefm.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.htt.app.xonefm.R;
import com.htt.app.xonefm.adapter.ListShowAdapter;
import com.htt.app.xonefm.config.Config;
import com.htt.app.xonefm.dialog.MyProgressDialog;
import com.htt.app.xonefm.jsonparsing.JSONParser;
import com.htt.app.xonefm.object.Show;
import com.htt.app.xonefm.server.Server;

public class ShowView implements OnClickListener{
	public boolean isCreate = false;
	
	private ListView listView;
	private Activity activity;

	private ImageButton btnRefresh;
	private TextView tvTitle;
	
	private View view;
	
	private int cateID = 13;
	
	public View createView(Activity context) {
		// TODO Auto-generated constructor stub
		activity = context;
		view = LayoutInflater.from(context).inflate(R.layout.list, null);
		
		listView = (ListView) view.findViewById(R.id.list);
		btnRefresh = (ImageButton) view.findViewById(R.id.list_header_refresh);
		tvTitle = (TextView) view.findViewById(R.id.list_header_title);
		
		
		if(btnRefresh.getVisibility() == View.GONE) {
			btnRefresh.setVisibility(View.VISIBLE);
		}
		
		btnRefresh.setOnClickListener(this);

		loadData();
		isCreate = true;
		
		return view;
	}
	public void loadData() {
		new AsyncTask<Void, Void, Void>() {
			
			private MyProgressDialog progress;
			private List<Show> data = new ArrayList<Show>();
			
			protected void onPreExecute() {
				progress = new MyProgressDialog(activity);
				progress.startProgress("", activity.getResources().getString(R.string.loading));
			};
			
			@Override
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				Server server = new Server(Config.URL_ListCategoryListened(cateID));
				JSONParser jsonParser = new JSONParser();
				data = jsonParser.getListShow(server.getJsonObject());
				return null;
			}
			
			protected void onPostExecute(Void result) {
				progress.stopProgress();
				if(data != null && data.size() > 0) {
					setData(data);
				} else
					Toast.makeText(activity, activity.getResources().getString(R.string.loading_error), 
							Toast.LENGTH_LONG).show();
			};
			
		}.execute();
	}
	
	public View getView() {
		return view;
	}
	
	/*
	 *  set data for display
	 */
	
	public void setData(List<Show> data) {
		if(data != null && data.size() > 0) {
			
			ListShowAdapter adapter = new ListShowAdapter(activity, R.layout.list_show_item, data);
			
			listView.setAdapter(adapter);
		}
		
	}
	
	public void setTitle(String title) {
		tvTitle.setText(title);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.list_header_refresh:
			loadData();
			break;
			
		default:
			break;
		}
	}

}
